package com.taoes.simpledocker.controller.container;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.github.dockerjava.api.model.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotEmpty;
import java.util.Collections;
import java.util.List;

/**
 * 运行新的容器的请求对象
 *
 * @author 枕上江南 zhoutao825638@vip.qq.com
 * @date 2021/12/5 1:18 上午
 */
@ApiModel
@AllArgsConstructor
@NoArgsConstructor
@Data
public class RunNewContainerRequest {


    /**
     * 使用的镜像ID
     */
    @ApiModelProperty(value = "容器id或容器tag", required = true)
    @NotEmpty
    private String imageId;

    /**
     * 容器名称
     */
    @ApiModelProperty(value = "指定的创建容器名称，不指定则随机生成", required = false)
    private String containerName;

    /**
     * 文件映射
     * */

    @ApiModelProperty(value = "BindsMap中key：宿主机目录，value：容器目录", required = false)
    private List<BindsMap> bindsMap;

    /**
     * 重启策略
     * */
    @ApiModelProperty(value = "失败重启次数，default：0", required = false)
    public Integer maximumRetryCount=0;

    /**
     * 网络模式
     * */
    @ApiModelProperty(value = "连接网络，default：bridge", required = false)
    public String networkMode="bridge";

    /**
     * 专业模式
     * */
    @ApiModelProperty(value = "专业模式，default：false", required = false)
    private boolean pro=false;

    /**
     * 环境变量
     */
    @ApiModelProperty(value = "容器系统环境变量", required = false)
    private List<EnvGroup> envGroup;

    /**
     * 端口绑定
     */
    @ApiModelProperty(value = "端口绑定，key：宿主机端口，value：容器端口", required = false)
    private  List<PortBound> portBound;

    /**
     * DNS
     */
    @ApiModelProperty(value = "dns，未用", required = false)
    private String dns;

    /**
     * 容器别名
     */
    @ApiModelProperty(value = "容器别名", required = false)
    private String alias;

    /**
     * 主机名
     */
    @ApiModelProperty(value = "主机名称", required = false)
    private String hostname;

    /**
     * cpu数量
     */
    @ApiModelProperty(value = "cup数量限制，default：0，无限制", required = false)
    private Long cpuCount=0L;

    /**
     * 内存大小
     */
    @ApiModelProperty(value = "内存限制，default：0，无限制", required = false)
    private Long Memory=0L;


    @NoArgsConstructor
    @Data
    public static class BindsMap {
        public String key;
        public String value;
    }

    @NoArgsConstructor
    @Data
    public static class PortBound {
        public Integer key;
        public Integer value;
    }


    @NoArgsConstructor
    @Data
    public static class EnvGroup {
        public String key;
        public String value;
    }

    public String getAlias() {

        return StrUtil.emptyIfNull(this.alias);

    }

    public List<String> findEnvList() {
        if (CollUtil.isEmpty(this.envGroup)) {
            return Collections.emptyList();
        }
        List<String> envs = CollUtil.newArrayList();
        for (EnvGroup e : this.envGroup) {
            if (StrUtil.isEmpty(e.getValue()) || StrUtil.isEmpty(e.getValue())) {
                return Collections.emptyList();
            }
            envs.add(String.format("%s=%s", e.getKey(), e.getValue()));
        }
        return envs;
    }


    public List<Bind>findBinds() {
        if (CollUtil.isEmpty(this.bindsMap)) {
            return Collections.emptyList();
        }
        List<Bind> Bindings = CollUtil.newArrayList();
        for (BindsMap b : this.bindsMap) {
            Bindings.add(new Bind(b.getKey(), new Volume(b.getValue())));
        }
        return Bindings;
    }

    public List<PortBinding> findPortBindings() {
        if (CollUtil.isEmpty(this.portBound)) {
            return Collections.emptyList();
        }
        List<PortBinding> portBindings = CollUtil.newArrayList();
        for (PortBound p : this.portBound) {
            portBindings.add(new PortBinding(Ports.Binding.bindPort(p.getKey()), new ExposedPort(p.getValue())));
        }
        return portBindings;
    }

}
